package com.lightdz.tshirtdz.bll;

import com.lightdz.tshirtdz.dal.impl.OrderDal;
import com.lightdz.tshirtdz.dal.impl.ProductDal;
import com.lightdz.tshirtdz.model.api.ApiPStyle;
import com.lightdz.tshirtdz.model.api.ApiResponse;
import com.lightdz.tshirtdz.model.api.ApiShoppingCar;
import com.lightdz.tshirtdz.model.entity.OrderRecordEntity;
import com.lightdz.tshirtdz.model.entity.ProductEntity;

import com.lightdz.tshirtdz.utils.LogHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @ Description：${description}
 * @ Author     ：zbl
 * @ Date       ：Created in 16:54 09/08/2018
 * @ Modified By：
 */
@Repository
public class OrderBll {

    @Autowired
    OrderDal orderDal;
    @Autowired
    ProductDal productDal;


    /**
     * 新增订单
     * @return
     */
    public ApiResponse<Boolean> addOrder(String wxId, long pid, int pCount, String pSize, String pColor, long receiveAddrId) {
        ApiResponse<Boolean> result = null;


        try {
            OrderRecordEntity entity = new OrderRecordEntity();
            entity.setBuystatus(0);
            entity.setCreatetime(new Timestamp(new Date().getTime()));
            entity.setEndtime(new Timestamp(new Date().getTime()));
            entity.setpCount(pCount);
            entity.setPid((int) pid);
            entity.setpSize(pSize);
            entity.setpColor(pColor);
            entity.setReceiceAddrId((int) receiveAddrId);
            entity.setWxid(wxId);
            boolean arrResult = orderDal.addOrderRecord(entity);

            result = new ApiResponse<>("OK", arrResult, true);
        } catch (Exception e) {
            e.printStackTrace();
            result.setData(false);
            result.setResult(false);
            result.setMsg(e.getMessage());
        }

        return result;
    }

    /**
     * 获取我的订单列表
     * @param wxId
     * @return
     */
    public ApiResponse<List<ApiShoppingCar>> getOrderList(String wxId) {
        ApiResponse<List<ApiShoppingCar>> apiResponse = ApiResponse.getNoResponse();

        List<ApiShoppingCar> result = new ArrayList<>();

        try {
            //拿到所有的收藏列表
            List<OrderRecordEntity> orderList = orderDal.getOrderList(wxId);
            if (!orderList.isEmpty()) {

                ApiShoppingCar shoppingCar = new ApiShoppingCar();

                for (OrderRecordEntity order : orderList) {
                    ProductEntity pEntity = productDal.getProduct(order.getPid());
                    if (pEntity != null) {
                        shoppingCar.setDesc(pEntity.getpDesc());
                        shoppingCar.setBannerPicUrl(pEntity.getpBannerPicUrl());
                        shoppingCar.setPrice((pEntity.getpPrice()));
                        /**
                         * 传订单号，用于删除订单
                         */
                        shoppingCar.setOrderId(order.getTid());

                        ApiPStyle pStyle = new ApiPStyle();
                        pStyle.setColorList(Arrays.asList(order.getpColor()));
                        pStyle.setpCount(order.getpCount());
                        pStyle.setSizeList(Arrays.asList(order.getpSize()));
                        pStyle.setPid(order.getPid());
                        pStyle.setWxId(wxId);

                        shoppingCar.setpStyle(pStyle);

                        result.add(shoppingCar);
                    }
                }
            }
            apiResponse = new ApiResponse<>(result);
        } catch (Exception e) {
            e.printStackTrace();
            LogHelper.error("OrderBll.getOrderList", e);
        }
        return apiResponse;
    }

    public ApiResponse<Boolean> deleteOrder(String wxId, long orderId) {
        boolean result = orderDal.deleteOrderRecord(wxId, orderId);
        return new ApiResponse<Boolean>(result);
    }
}
